﻿using DNFImagePacks2Manager.Helpers;
using System.Collections.Generic;
using System.IO;

namespace DNFImagePacks2Manager.NPK
{
    public class ColorPanel
    {
        public static readonly byte[] transparent = [255, 255, 255, 0];

        public int Count => Colors.Count;
        public List<byte[]> Colors { get; set; } = [];

        public ColorPanel(BinaryReader reader)
        {
            byte[] countBytes = reader.ReadBytes(4);
            int count = ByteHelper.Bytes2Int(countBytes);

            for (int i = 0; i < count; i++)
            {
                byte[] temp = reader.ReadBytes(4);
                (temp[2], temp[0]) = (temp[0], temp[2]);
                Colors.Add(temp);
            }
        }

        public byte[] GetColors(int index) {
            if(index < Count)
            {
                return Colors[index];
            }
            else
            {
                return transparent;
            }
        }

        public byte[] ReadImageBytes(List<byte> colorIndexs)
        {
            List<byte> imageData = [];
            foreach (byte index in colorIndexs)
            {
                imageData.AddRange(GetColors(index));
            }
            return [.. imageData];
        }

        public byte[] GetBytes()
        {
            List<byte> byteList = [];
            byteList.AddRange(ByteHelper.Int2Bytes(Count));
            foreach (var color in Colors)
            {
                byteList.Add(color[2]);
                byteList.Add(color[1]);
                byteList.Add(color[0]);
                byteList.Add(color[3]);
            }
            return [.. byteList];
        }
    }
}
